package cn.com.jslh.nfcpro.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by Administrator on 2016/8/12.
 */
public class TimeUtil {
    public static String getNowTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(new Date());
    }

    /**
     * 判断比较时间是否小于等于当前日期
     * @param dayStr
     * @return
     */
    public static boolean isSmallOrEqThenNowDay(String dayStr){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        boolean result =false;
        try
        {
            Date complareDate = df.parse(dayStr);
            Date nowDate = new Date();
            if(nowDate.getTime()- complareDate.getTime()>=0){
                result =true;
            }
        }
        catch (Exception e)
        {
            e.fillInStackTrace();
        }
        return  result;
    }

    /**
     * 获取时间间隔
     * @param startDay
     * @param endDay
     * @return
     */
    public static int getDays(String startDay, String endDay){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int day =0;
        try
        {
            Date endDate = df.parse(endDay);
            Date startDate = df.parse(startDay);
            long diff = endDate.getTime()- startDate.getTime();//这样得到的差值是微秒级别
            long days = diff / (1000 * 60 * 60 * 24);
            day =(int)days;
//            long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
//            long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
//            System.out.println(""+days+"天"+hours+"小时"+minutes+"分");
        }
        catch (Exception e)
        {
            e.fillInStackTrace();
        }
        return day;
    }
    public static int getDays(String startDay){
        String now = TimeUtil.getNowTime();
        startDay =startDay +" 00:00:00";
        return getDays(startDay,now);
    }

    public static String getDayName(String startDay, int days) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date =df.parse(startDay);
        String datenext = df.format(new Date(date.getTime() + (long) days * 24 * 60 * 60 * 1000));
        return  datenext;
    }

    /**
     * 计算两个日期型的时间相差多少时间
     * @param startDate  开始日期
     * @param endDate    结束日期
     * @return
     */
    public String twoDateDistance(Date startDate, Date endDate){

        if(startDate == null ||endDate == null){
            return null;
        }
        long timeLong = endDate.getTime() - startDate.getTime();
        if (timeLong<60*1000)
            return timeLong/1000 + "秒前";
        else if (timeLong<60*60*1000){
            timeLong = timeLong/1000 /60;
            return timeLong + "分钟前";
        }
        else if (timeLong<60*60*24*1000){
            timeLong = timeLong/60/60/1000;
            return timeLong+"小时前";
        }
        else if (timeLong<60*60*24*1000*7){
            timeLong = timeLong/1000/ 60 / 60 / 24;
            return timeLong + "天前";
        }
        else if (timeLong<60*60*24*1000*7*4){
            timeLong = timeLong/1000/ 60 / 60 / 24/7;
            return timeLong + "周前";
        }
        else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
            return sdf.format(startDate);
        }
    }
}
